package com.headwire.kw.helpers.qxml;

import java.util.ArrayList;

public class QXElement {
	protected ArrayList<QXAttribute> attrs = null;
	protected ArrayList<QXElement> children = null;
	private final QXElement parent;
	protected StringBuilder text = null;
	private String name;
	
	public QXElement(QXElement parent, String name) {
		this.parent = parent;
		this.name = name;
		if(parent != null) {
			parent.addChild(this);
		}
	}
	
	public void addChild(QXElement child) {
		if(children == null) {
			children = new ArrayList<QXElement>();
		}
		children.add(child);
	}
	
	public void addAttribute(String name, String value) {
		addAttribute(new QXAttribute(name,value));
	}
	
	public void addAttribute(QXAttribute attr) {
		if(attrs == null) {
			attrs = new ArrayList<QXAttribute>();
		}
		attrs.add(attr);
	}
	
	public QXElement getParent() {
		return parent;
	}
	
	public void addText(String txt) {
		if(text == null) {
			text = new StringBuilder();
		}
		text.append(txt);
	}
	
	public String getText() {
		if(text == null) {
			return "";
		}
		return text.toString();
	}
	
	public String getName() {
		return name;
	}
	
	public ArrayList<QXElement> getChildNodes() {
		return children;
	}
}
